import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';

import 'index.dart';
import 'widgets/index.dart';
import 'package:playtruly/common/routers/names.dart';

class SettingPage extends GetView<SettingController> {
  const SettingPage({super.key});

  // 主视图
  Widget _buildView() {
    return ListView(
      children: [
        // 软件版本
        _buildItem(
          title: "软件版本",
          trailing: Text(
            controller.appVersion,
            style: const TextStyle(color: Colors.grey, fontSize: 14),
          ),
        ),
        _buildDivider(),
        
        // 用户手册
        _buildItem(
          title: "用户手册",
          hasArrow: true,
          onTap: () => Get.toNamed(RouteNames.systemAgreementPrivacyPolicy, arguments: {'type': 'manual'}),
        ),
        _buildDivider(),
        
        // 隐私政策
        _buildItem(
          title: "隐私政策",
          hasArrow: true,
          onTap: () => Get.toNamed(RouteNames.systemAgreementPrivacyPolicy, arguments: {'type': 'privacy'}),
        ),
        _buildDivider(),
        
        // 检查更新
        _buildItem(
          title: "检查更新",
          onTap: controller.checkUpdate,
        ),
        _buildDivider(),
        
        // 清空缓存
        _buildItem(
          title: "清空缓存",
          onTap: controller.clearCache,
        ),
        _buildDivider(),
        
        // 举报信息
        _buildItem(
          title: "举报信息",
          hasArrow: true,
          onTap: () => Get.to(() => const ReportPage()),
        ),
        _buildDivider(),
      ],
    );
  }
  
  // 构建分隔线
  Widget _buildDivider() {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 16),
      child: const Divider(height: 1),
    );
  }
  
  // 构建列表项
  Widget _buildItem({
    required String title,
    Widget? trailing,
    bool hasArrow = false,
    VoidCallback? onTap,
  }) {
    return InkWell(
      onTap: onTap,
      child: Container(
        padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 16),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text(
              title,
              style: const TextStyle(
                fontSize: 16,
                fontWeight: FontWeight.normal,
              ),
            ),
            Row(
              children: [
                trailing ?? const SizedBox(),
                if (hasArrow) ...[
                  const SizedBox(width: 8),
                  const Icon(
                    Icons.arrow_forward_ios,
                    size: 16,
                    color: Colors.grey,
                  ),
                ]
              ],
            ),
          ],
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<SettingController>(
      init: SettingController(),
      id: "setting",
      builder: (_) {
        return Scaffold(
          backgroundColor: Colors.white,
          appBar: AppBar(
            title: const Text("设置"),
            centerTitle: true,
            backgroundColor: Colors.white,
            foregroundColor: Colors.black,
            elevation: 0,
            systemOverlayStyle: const SystemUiOverlayStyle(
              statusBarColor: Colors.transparent,
              statusBarIconBrightness: Brightness.dark,
            ),
          ),
          body: SafeArea(
            child: _buildView(),
          ),
        );
      },
    );
  }
}
